home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / CBKEYLAS.C < prev    next >
Text File  |  1991-09-23  |  3KB  |  144 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbkeylas.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDLIB
  11. #include <stdlib.h>
  12. #endif
  13. #ifdef AC_STRING
  14. #include <string.h>
  15. #endif
  16.  
  17. /* library headers */
  18. #include <blkio.h>
  19. #include <btree.h>
  20. #include <lseq.h>
  21.  
  22. /* local headers */
  23. #include "cbase_.h"
  24.  
  25. /*man---------------------------------------------------------------------------
  26. NAME
  27.      cbkeylast - last cbase key
  28.  
  29. SYNOPSIS
  30.      #include <cbase.h>
  31.  
  32.      int cbkeylast(cbp, field)
  33.      cbase_t *cbp;
  34.      int field;
  35.  
  36. DESCRIPTION
  37.      The cbkeylast function positions the key cursor of the specified
  38.      field in cbase cbp to the last key.  The record cursor of cbp is
  39.      positioned to the record associated with that key.  Other key
  40.      cursors are not affected.
  41.  
  42.      cbkeylast will fail if one or more of the following is true:
  43.  
  44.      [EINVAL]       cbp is not a valid cbase pointer.
  45.      [EINVAL]       field is not a valid field number for
  46.                     cbase cbp.
  47.      [CBELOCK]      cbp is not locked.
  48.      [CBENKEY]      field is not a key.
  49.      [CBENOPEN]     cbp is not open.
  50.      [CBENREC]      cbp is empty.
  51.  
  52. SEE ALSO
  53.      cbkeyfirst, cbkeynext, cbkeyprev, cbreccnt, cbreclast.
  54.  
  55. DIAGNOSTICS
  56.      Upon successful completion, a value of 0 is returned.  Otherwise,
  57.      a value of -1 is returned, and errno set to indicate the error.
  58.  
  59. ------------------------------------------------------------------------------*/
  60. #ifdef AC_PROTO
  61. int cbkeylast(cbase_t *cbp, int field)
  62. #else
  63. int cbkeylast(cbp, field)
  64. cbase_t *cbp;
  65. int field;
  66. #endif
  67. {
  68.     void *    buf    = NULL;
  69.     cbrpos_t cbrpos    = NIL;
  70.     lspos_t    lspos    = NIL;
  71.  
  72.     /* validate arguments */
  73.     if (!cb_valid(cbp)) {
  74.         errno = EINVAL;
  75.         return -1;
  76.     }
  77.  
  78.     /* check if not open */
  79.     if (!(cbp->flags & CBOPEN)) {
  80.         errno = CBENOPEN;
  81.         return -1;
  82.     }
  83.  
  84.     /* validate arguments */
  85.     if (field < 0 || field >= cbp->fldc) {
  86.         errno = EINVAL;
  87.         return -1;
  88.     }
  89.  
  90.     /* check if field not a key */
  91.     if (!(cbp->fldv[field].flags & CB_FKEY)) {
  92.         errno = CBENKEY;
  93.         return -1;
  94.     }
  95.  
  96.     /* check if not locked */
  97.     if (!(cbp->flags & CBLOCKS)) {
  98.         errno = CBELOCK;
  99.         return -1;
  100.     }
  101.  
  102.     /* check if cbase empty */
  103.     if (lsreccnt(cbp->lsp) == 0) {
  104.         errno = CBENREC;
  105.         return -1;
  106.     }
  107.  
  108.     /* set key cursor to last key */
  109.     if (btlast(cbp->btpv[field]) == -1) {
  110.         CBEPRINT;
  111.         return -1;
  112.     }
  113.  
  114.     /* get record position */
  115.     if (btkeysize(cbp->btpv[field]) != (cbp->fldv[field].len + sizeof(cbrpos_t))) {
  116.         CBEPRINT;
  117.         errno = CBEPANIC;
  118.         return -1;
  119.     }
  120.     buf = calloc((size_t)1, btkeysize(cbp->btpv[field]));
  121.     if (buf == NULL) {
  122.         CBEPRINT;
  123.         errno = ENOMEM;
  124.         return -1;
  125.     }
  126.     if (btgetk(cbp->btpv[field], buf) == -1) {
  127.         CBEPRINT;
  128.         free(buf);
  129.         return -1;
  130.     }
  131.     memcpy(&cbrpos, (char *)buf + cbp->fldv[field].len, sizeof(cbrpos));
  132.     free(buf);
  133.     buf = NULL;
  134.  
  135.     /* set record cursor */
  136.     lspos = cbrpos;
  137.     if (lssetcur(cbp->lsp, &lspos) == -1) {
  138.         CBEPRINT;
  139.         return -1;
  140.     }
  141.  
  142.     return 0;
  143. }
  144.